package com.easylinkin.linkappapi.danger.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.danger.entity.Danger;
import com.easylinkin.linkappapi.danger.entity.DangerType;
import com.easylinkin.linkappapi.danger.entity.vo.DangerTypeNode;
import com.easylinkin.linkappapi.danger.service.DangerService;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.List;

/**
 * Danger表控制层
 *
 * @author CodeGenerator
 * @date 2022/07/21
 */
@RestController
@RequestMapping("danger")
public class DangerController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private DangerService appDangerService;

    /**
     * 分页查询所有数据
     *
     * @param requestModel 查询分页对象
     * @return 所有数据
     */
    @PostMapping("getPage")
    @ApiOperation("查询分页")
    public RestMessage selectPage(@RequestBody RequestModel<Danger> requestModel) {
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        IPage<Danger> record = appDangerService.selectPage(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder(record).build();
    }

    @PostMapping("selectList")
    @ApiOperation("查询列表")
    public RestMessage selectList(@RequestBody Danger dan) {
        Assert.notNull(dan, "body参数不能为空");
        List<Danger> record = appDangerService.selectList(dan);
        return RestBuilders.successBuilder(record).build();
    }

    /**
     * @param add   是否新增 同步新增 1-是，0-否
     * @param cover 是否覆盖 同步覆盖 1-是，0-否
     * @return
     */
    @PostMapping("synchronizeData")
    @ApiOperation("同步企业级隐患库")
    public RestMessage synchronizeData(@RequestParam("add") Integer add, @RequestParam("cover") Integer cover) {
        appDangerService.synchronizeData(add, cover);
        return RestBuilders.successBuilder().build();
    }

    @PostMapping("selectListWithType")
    @ApiOperation("查询")
    public RestMessage selectListWithType(@RequestBody DangerType dangerType) {
        Assert.notNull(dangerType, "customQueryParams 不能为空");
        List<DangerTypeNode> record = appDangerService.selectListWithType(dangerType);
        return RestBuilders.successBuilder(record).build();
    }


    @PostMapping("selectTreeWithType")
    @ApiOperation("查询隐患库+隐患类型组成的树")
    public RestMessage selectTreeWithType(@RequestBody DangerType dangerType) {
        Assert.notNull(dangerType, "customQueryParams 不能为空");
        List<DangerTypeNode> record = appDangerService.selectTreeWithType(dangerType);
        return RestBuilders.successBuilder(record).build();
    }


        /**
         * 通过主键查询单条数据
         *
         * @param id 主键
         * @return 单条数据
         */
    @GetMapping("{id}")
    @ApiOperation("查询单条")
    public RestMessage selectOne(@PathVariable Serializable id) {
        return RestBuilders.successBuilder((this.appDangerService.getOneById(id))).build();
    }

    /**
     * 新增数据
     *
     * @param appDanger 实体对象
     * @return 新增结果
     */
//    @PostMapping
//    @ApiOperation("新增")
//    public RestMessage insert(@RequestBody Danger appDanger) {
//        return RestBuilders.successBuilder().success((this.appDangerService.saveOne(appDanger))).build();
//    }
//
//    /**
//     * 修改数据
//     *
//     * @param appDanger 实体对象
//     * @return 修改结果
//     */
//    @PutMapping
//    @ApiOperation("修改单条")
//    public RestMessage update(@RequestBody Danger appDanger) {
//        return RestBuilders.successBuilder().success(this.appDangerService.updateOne(appDanger)).build();
//    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    @ApiOperation("批量删除")
    public RestMessage delete(@RequestParam("idList") List<Long> idList) {
        return RestBuilders.successBuilder().success(this.appDangerService.deleteByIds(idList)).build();
    }

    @PostMapping("export")
    @ApiOperation("导出")
    public void export(@RequestBody Danger appDanger, HttpServletRequest request, HttpServletResponse response) {
        appDangerService.export(appDanger, request, response);
    }
}

